package com.example.easyutils.openprize;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.easyutils.R;
import com.example.easyutils.garbagepage.Main;

import java.util.List;
import java.util.Map;

public class OpenRecycleView extends RecyclerView.Adapter{
    private List mLst;
    private Context context;
    private String code;
    OpenCode openCode = new OpenCode();

    public OpenRecycleView(List mList, Context openCode,String code) {
        this.context = openCode;
        this.mLst = mList;
        this.code = code;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int layout = R.layout.open_code_item;
        if(code.equals("showCode")){
            layout = R.layout.show_opencode_item;
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(layout,parent,false);
        ViewHoder viewHoder = new ViewHoder(view);
        return viewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if(code.equals("showCode")){
            Log.d("list",mLst+"");
            Log.d("list1",((Map)mLst.get(position)).get("opencode")+"");
            ((ViewHoder)holder).openCode.setText("开奖号码:"+((Map)mLst.get(position)).get("opencode").toString());
            ((ViewHoder)holder).openExpect.setText("期数:"+((Map)mLst.get(position)).get("expect").toString());
            ((ViewHoder)holder).openTime.setText("日期:"+((Map)mLst.get(position)).get("opentime").toString());
            return;
        }
        ((ViewHoder)holder).textView.setText(((Map)mLst.get(position)).get("text").toString());
        Glide
                .with(context)
                .load(((Map)mLst.get(position)).get("image"))
                .into(((ViewHoder)holder).imageView);

        ((ViewHoder)holder).linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("position",position+"");
                openCode.getOpenCode(((Map)mLst.get(position)).get("code").toString());
            }
        });

    }



    @Override
    public int getItemCount() {
        return mLst.size();
    }

    class ViewHoder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView,openTime,openExpect,openCode;
        private LinearLayout linearLayout;
        public ViewHoder(@NonNull View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);
            textView = (TextView)itemView.findViewById(R.id.textView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.picLinearLayout);

            openTime = (TextView)itemView.findViewById(R.id.openTime);
            openExpect = (TextView)itemView.findViewById(R.id.expect);
            openCode = (TextView)itemView.findViewById(R.id.openCode);

        }
    }
}
